package site.lyweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import site.lyweb.constant.Constant;
import site.lyweb.domain.dto.*;
import site.lyweb.domain.query.TeacherCourseQuery;
import site.lyweb.domain.query.TeacherQuery;
import site.lyweb.mapper.TeacherCourseMapper;
import site.lyweb.mapper.TeacherMapper;
import site.lyweb.service.TeacherCourseService;
import site.lyweb.service.TeacherService;
import site.lyweb.utils.ExtBeanUtils;
import site.lyweb.utils.UUIDUtils;

import java.util.List;

/**
* @author 深山老林
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2024-06-19 20:53:32
*/
@Service
public class TeacherCourseServiceImpl implements TeacherCourseService {
    @Resource
    private TeacherCourseMapper teacherCourseMapper;
    /**
     * 分页 + 搜索
     *
     * @param current
     * @param teacherCourseQuery
     * @return
     */
    @Override
    public PageDTO<TeacherCourseDTO> getTeacherCourseByPage(Integer current, TeacherCourseQuery teacherCourseQuery) {
        //分页查询分两个查询，1、查符合条件的数据总条数；2、查询当前页的数据
        int totalRow = teacherCourseMapper.selectByCount(teacherCourseQuery);
        int start = (current - 1) * Constant.PAGE_SIZE;
        List<TeacherCourse> teacherCoursesList = teacherCourseMapper.selectByPage(teacherCourseQuery);
        int totalPage = totalRow / Constant.PAGE_SIZE;
        if (totalRow % Constant.PAGE_SIZE > 0) { //如果除不尽，总页数就需要加1
            totalPage ++;
        }
        //需要把 List<TUser> 转换为 List<UserInfoDTO>
        List<TeacherCourseDTO> teacherCoursesDTOList = ExtBeanUtils.copyPropertiesForList(teacherCoursesList, TeacherCourseDTO.class);

        return new PageDTO<>(current, Constant.PAGE_SIZE, totalRow, totalPage, teacherCoursesDTOList);

    }

    @Override
    public boolean removeBatchByIds(List<String> idList) {
        try {
            return teacherCourseMapper.removeBatchByIds(idList);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean removeById(String id) {
        try {
            return teacherCourseMapper.removeById(id);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean save(String id, String teacherId, String courseId) {
        try {
            //无id：插入方法
            if(id==null || id.isEmpty()){
                id = UUIDUtils.getUUID();
                return teacherCourseMapper.insert(id, teacherId, courseId);
            } else {
                return teacherCourseMapper.update(id, teacherId, courseId);
            }
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TeaCourseRelation getById(String id) {
        try {
            return teacherCourseMapper.getById(id);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return null;
        }
    }

}




